package top.laoshuzi.cloudplatform.service;


import android.hardware.Sensor;
import android.hardware.SensorManager;

import com.google.android.things.userdriver.UserDriverManager;
import com.google.android.things.userdriver.UserSensor;
import com.google.android.things.userdriver.UserSensorDriver;
import com.google.android.things.userdriver.UserSensorReading;

import java.io.IOException;

import top.laoshuzi.cloudplatform.tools.L;

/**
 * Created by mouse on 2017/12/21.
 */

public class Mpu6050Driver implements AutoCloseable {

    public static final String DRIVER_NAME = "top.laoshuzi.sensor.mpu6050";
    public static final String DRIVER_TYPE = "top.laoshuzi.sensor";
    private static final int DRIVER_VERSION = 1;

    private Mpu6050 mDevice;
    private UserSensor mSensor;

    public Mpu6050Driver(String bus) throws IOException {
        this(new Mpu6050(bus));
    }

    public Mpu6050Driver(Mpu6050 mpu6050) {
        this.mDevice = mpu6050;
        if (mSensor == null) {
            mSensor = build(mDevice);
        }
    }

    @Override
    public void close() throws Exception {
        unregister();
        if (mDevice != null) {
            try {
                mDevice.close();
            } finally {
                mDevice = null;
            }
        }
    }

    public void register() throws IOException {
        if (mDevice == null) {
            throw new IllegalStateException("cannot registered closed driver");
        }
        L.i("mpu6050 is register");
        if (mSensor != null) {
            UserDriverManager.getManager().registerSensor(mSensor);
        }
    }

    /**
     * Unregister the driver from the framework.
     */
    public void unregister() throws IOException {
        L.i("mpu6050 is unregister");
        if (mSensor != null) {
            UserDriverManager.getManager().unregisterSensor(mSensor);
            mSensor = null;
        }
    }

    static UserSensor build(final Mpu6050 mpu6050) {
        return new UserSensor.Builder()
                .setName(DRIVER_NAME)
                .setVersion(DRIVER_VERSION)
                .setCustomType(
                        Sensor.TYPE_DEVICE_PRIVATE_BASE,
                        DRIVER_TYPE,
                        Sensor.REPORTING_MODE_CONTINUOUS)
                .setDriver(new UserSensorDriver() {
                    @Override
                    public UserSensorReading read() throws IOException {
//                        float[] values = new float[7];
//                        float[] acc = mpu6050.readAcceleration();
//                        float[] gyro = mpu6050.readAngleSpeed();
//                        float temp = mpu6050.readTemperature();
//                        System.arraycopy(acc, 0, values, 0, 3);
//                        System.arraycopy(gyro, 0, values, 3, 3);
//                        values[6] = temp;
                        float[] values = mpu6050.readSimple();
                        return new UserSensorReading(
                                values,
                                SensorManager.SENSOR_STATUS_ACCURACY_HIGH
                        );
                    }

                    @Override
                    public void setEnabled(boolean enabled) throws IOException {
                        if (enabled) {
                            L.d("激活");
                            mpu6050.setPowerMode(Mpu6050.POWE_MODE_ACTIVE);
                        } else {
                            L.d("禁用");
                            mpu6050.setPowerMode(Mpu6050.POWE_MODE_STANDBY);
                        }
                    }
                })
                .build();
    }

}
